package com.my.screenadapter.MyRecyclerHeader;

import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.my.screenadapter.Model.Person;
import com.my.screenadapter.MyRecyclerHeader.adapter.SonRvNormalAdapter;
import com.my.screenadapter.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 张英杰 on 2016/11/15.
 *
 * 2019-11-21 19:09:32 最佳实践
 * recyclerview
 * 乱序原因：view 复用，显示错位
 *
 * https://blog.51cto.com/liuxudong1001/1899826
 * 因为 复用
 *
 * 虽然 item 复用，但是bind position 导致holder 对象不同，view 还是那个view id 一致 。但是 position是不同的，
 *
 * bindview时，edittext 先 settag,
 * todo textwatch 监听回调，搞不清，现在具体是那个 position
 * 所以 gettag 获得真实的bind 位置，保存数据。
 *
 * todo 2019-11-21 19:10:19 待处理问题， 多个 edittext 滑动焦点问题。
 */
public class RecyclerActivity extends AppCompatActivity {
    @BindView(R.id.rv)
    RecyclerView rv;
    private SonRvNormalAdapter mAdapter;

    //scdn头文件添加地址
//    http://blog.csdn.net/lmj623565791/article/details/51854533
//    Android RecyclerView 使用完全解析 体验艺术般的控件
//    http://blog.csdn.net/lmj623565791/article/details/45059587
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myrecycler);
        ButterKnife.bind(RecyclerActivity.this);
        initRv();
        initData();
    }

    private void initData() {
        List<Person> data = new ArrayList<>();
        for (int i = 'a'; i <= 'z'; i++) {
            Person person = new Person();
            person.setAge((char) i + "");
            person.setGender(i % 2 == 0 ? "女" : "男");
//            person.setName("第" + Math.round(100) + "号");
            person.setName("第" + i + "号");
            data.add(person);
        }
        mAdapter.setDatas(data);
    }

    private void initRv() {
        //rv的csdn博客
//        http://blog.csdn.net/lmj623565791/article/details/45059587
        mAdapter = new SonRvNormalAdapter(this);
//      设置布局管理器
//        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
//        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
//        GridLayoutManager grManager = new GridLayoutManager(this,3);
//        rv.setLayoutManager(layoutManager);
//        rv.setLayoutManager(grManager);

       rv.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));


//        添加分割线：
//        rv.addItemDecoration(new MyItemDecoration(this, MyItemDecoration.VERTICAL_LIST));
//        rv.addItemDecoration(new DividerItemDecoration(
//                RecyclerActivity.this, DividerItemDecoration.HORIZONTAL));
        rv.addItemDecoration(new DividerGridItemDecoration(this));

//设置adapter
        rv.setAdapter(mAdapter);

//设置Item增加、移除动画
        rv.setItemAnimator(new DefaultItemAnimator());
    }

}
